/**
 * @copyright 2018 sinping.com 北京芯平科技股份有限公司. All rights reserved.
 * 本内容仅限于北京芯平科技股份有限公司内部传阅，禁止外泄以及用于其他的商业目的.
 */
package com.sinping.qh.api.admin.plan;

import com.sinping.qh.api.support.Const;
import com.sinping.qh.api.support.FrontPage;
import com.sinping.qh.api.support.ResDto;
import com.sinping.qh.service.admin.ICheckPlanAdminService;
import com.sinping.qh.utils.mapper.JsonMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author yuyue
 * @create 2018/5/24
 */
@Api(value = "Admin检查管理", description = "Admin检查管理")
@RestController
@RequestMapping( value = Const.ADMIN_URI ,name = "检查计划")
@Slf4j
public class PlanEndPoint {

    private final static Logger logger = LoggerFactory.getLogger(PlanEndPoint.class);

    @Autowired
    private ICheckPlanAdminService iCheckPlanAdminService;

    @ApiOperation(value = "获取计划列表", notes = "获取计划列表")
    @RequestMapping(value = "plan",method = RequestMethod.GET, name = "获取计划列表")
    public ResDto listPage(HttpServletRequest request, @ApiParam(name = "msg", value = "{\"page_size\":10,\"page_current\":1,\"order_column\":null,\"order_by\":null" +
            ", \"condition\":{\"check_unit_id\":\"log\",\"check_building_id\":\"1\",\"start_date\":\"1\",\"end_date\":\"1\"}}", required = true)
                           @RequestParam(required = true) String msg) {
        try {
            logger.info("入参 {}", msg);
            FrontPage page = JsonMapper.defaultMapper().fromJson(msg, FrontPage.class);
            if (page == null) {
                log.info("参数为空或有不正确的数据格式");
                return new ResDto(Const.PARAM_ERROR, "msg.param.error");
            }
            return iCheckPlanAdminService.getPlanList(request,page);
        } catch (Exception e) {
            logger.error("数据解析异常{}", e);
            ResDto resDto = new ResDto(Const.SERVER_ERROR, "数据解析异常");
            return resDto;
        }
    }
    /**
     * 获取计划详情
     *
     * @param id
     * @return ResDto
     */
    @ApiOperation(value = "获取计划详情")
    @RequestMapping(value = "plan/{id}", method = RequestMethod.GET, name = "获取计划详情")
    public ResDto getPlanDetail(@ApiParam(name = "id", value = "245ad13d6ad4") @PathVariable String id) {

        if (StringUtils.isEmpty(id)) {
            logger.info("必传参数 :{}", "id");
            return new ResDto(Const.PARAM_ERROR, "id");
        }

        return iCheckPlanAdminService.getPlanDetail(id);


    }
}